﻿using System;
using System.Data;
using System.Text;
using Token.Piker.BLL;
using Token.Piker.Core;

namespace Token.Piker.Web
{
    public partial class album_list_iframe : PikerBasePage
    {
        public NewsAlbumBLL albumBLL = new NewsAlbumBLL();

        public string pager = "";

        public int pagerSize = 6;

        public int currentpage;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OutputPager();
                BindData();
            }
        }

        public string put = "";
        private void OutputPager()
        {
            //List<NewsAlbum> list = albumBLL.GetModelList("1=1");
            int page = 1;
            string date = "";
            string pre = "0";
            if (Request.QueryString["page"] ==null )
            {
                page = 1;
            }
            else
            {
                try
                {
                    page = int.Parse(Request.QueryString["page"].ToString());
                }
                catch (System.Exception ex)
                {
                    page = 1;
                }
            }

            if (Request.QueryString["date"] == null)
            {
                date = DateTime.Now.ToString("yyyy-MM");
            }
            else
            {
                date = Request.QueryString["date"].ToString();
            }

            if (Request.QueryString["pre"] == null)
            {
                pre = "0";
            }
            else
            {
                pre = Request.QueryString["pre"].ToString();
            }

            //判断page cout
            StringBuilder sb_query = new StringBuilder();
            string date_begin = Convert.ToDateTime(date + "-01").ToString("yyyy-MM-dd");
            string date_end = Convert.ToDateTime(date + "-01").AddMonths(1).ToString("yyyy-MM-dd");
            int recordCount = 0;
            //int recordCount = albumBLL.GetRecordCount(" ShotTime between '" + date_begin + "' and '" + date_end + "' ");
            if (pre == "0")
            {
                recordCount = albumBLL.GetRecordCount(" datediff(month,ShotTime,'" + date + "-01" + "') = 0  ");
            }
            else  // pre == "1"
            {
                recordCount = albumBLL.GetRecordCount(" datediff(month,ShotTime,'" + date + "-01" + "') >= 0  ");
            }
            
            
            //判断是否没有记录
            if (recordCount == 0) 
            {
                put = "暂时没有照片";
            }
            

            int pagecount = recordCount  / pagerSize + 1;


            StringBuilder sb = new StringBuilder();
            int prepage = page == 1 ? 1 : page - 1;
            currentpage = page;
            
            sb.AppendFormat("<li><a href=\"album_list_iframe.aspx?page={0}&date={1}&pre={2}\"><</a></li>", prepage, date, pre);

            int page1 = page == 1 ? 1 : page - 1;
            int page2 = page == 1 ? 2 : page;
            int page3 = page == 1 ? 3 : page + 1;

            if(page1<=pagecount)
                sb.AppendFormat("<li><a href=\"album_list_iframe.aspx?page={0}&date={1}&pre={2}\">{0}</a></li>", page1, date, pre);
            if (page2 <= pagecount)
                sb.AppendFormat("<li><a href=\"album_list_iframe.aspx?page={0}&date={1}&pre={2}\">{0}</a></li>", page2, date, pre);
            if (page3 <= pagecount)
                sb.AppendFormat("<li><a href=\"album_list_iframe.aspx?page={0}&date={1}&pre={2}\">{0}</a></li>", page3, date, pre);

            int nextpage = prepage + 2;
            if (nextpage > pagecount)
            {
                nextpage = pagecount;
            }

            sb.AppendFormat("<li><a href=\"album_list_iframe.aspx?page={0}&date={1}&pre={2}\">></a></li>", nextpage, date, pre);

            pager = sb.ToString();
            

        }

        protected void BindData()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("1=1 AND ");
            string date = "";
            string pre = "0";

            if (Request.QueryString["date"] == null)
            {
                date = DateTime.Now.ToString("yyyy-MM");
            }
            else
            {
                date = Request.QueryString["date"].ToString();
            }

            if (Request.QueryString["pre"] == null)
            {
                pre = "0";
            }
            else
            {
                pre = Request.QueryString["pre"].ToString();
            }
            if (pre == "0")
            {
                DateTime dt = DateTime.Parse(date + "-01");
                sb.AppendFormat(" datediff(month,ShotTime,'" + dt.ToString("yyyy-MM-dd") + "') = 0  ");
                DataSet ds = albumBLL.GetListByPage(sb.ToString(), "id", (currentpage - 1) * pagerSize + 1, (currentpage) * pagerSize);
                ablist.DataSource = ds;
                ablist.DataBind();
            }
            else if (pre == "1")
            {
                DateTime dt = DateTime.Parse(date + "-01");
                sb.AppendFormat(" datediff(month,ShotTime,'" + dt.ToString("yyyy-MM-dd") + "') >= 0  ");
                DataSet ds = albumBLL.GetListByPage(sb.ToString(), "id", (currentpage - 1) * pagerSize + 1, (currentpage) * pagerSize);
                ablist.DataSource = ds;
                ablist.DataBind();
            }
            
        }

    }
}